<?php 
if (!isset($_SESSION)) {
  session_start();
}
define("CCTRL", "content");
define("CCTRLNAME", "content");
define("TOKEN", "bb6222905ef8419183b5437779497596");
include APPPATH . 'libraries/defu.php';
class content extends CI_Controller  {

        /**
        * Index Page for this controller.
        *
        * Maps to the following URL
        * 		http://example.com/index.php/welcome
        *	- or -  
        * 		http://example.com/index.php/welcome/index
        *	- or -
        * Since this controller is set as the default controller in 
        * config/routes.php, it's displayed at http://example.com/
        *
        * So any other public methods not prefixed with an underscore will
        * map to /index.php/welcome/<method_name>
        * @see http://codeigniter.com/user_guide/general/urls.html
        */
       protected $configs;
       function __construct()
        {
            parent::__construct();
            $this->load->helper('url');
            $this->load->model('backend/content_model');
            $this->content_model=new content_model();
            $this->load->model('backend/backend_model');
            $this->backend_model=new backend_model(array(
                "_table"    =>"tbl_content",
                "_id"       =>"ID",
                "_status"   =>"Status",
                "_insert"   =>"Insert",
                "_delete"   =>"Delete",
                "_update"   =>"Update",
            ));
            $this->load->model('colmod/content_colmod');
            $this->colModel=new content_colmod();
            $this->load->library('smarty3');
            $this->smarty=new CI_Smarty3();
            $this->smarty->error_reporting = E_ALL & ~E_NOTICE;
            if(!isset($_SESSION["ADP"][CCTRL]["display"]))$_SESSION["ADP"][CCTRL]["display"]=0;
        }
        function index(){
            if(!isset($_SESSION["ADP"]["USER"])){
                $this->smarty->display("backend/00_login"); 
                return;
            }
            if(!checkAuthority(CCTRLNAME)){
                $this->smarty
                        ->assign('_SESSION', $_SESSION)
                        ->display("backend/00_accessdenied");
                return;
            }
            $this->smarty
                    ->assign('_SESSION', $_SESSION)
                    ->display("backend/".CCTRL."/".CCTRL);
        }
        function gridscript(){
            //unset($_SESSION["ADP"][CCTRL."-JQX"]);
            if(isset($_SESSION["ADP"][CCTRL."-JQX"]["colModel"]))
                $colModel=$_SESSION["ADP"][CCTRL."-JQX"]["colModel"];
            else{
                $this->load->model("backend/type_model");
                $this->type_model=new type_model();
                $tmp=$this->type_model->getByType("content");
                $atypes=null;
                foreach ($tmp as $v){
                    $atypes[]=$v->_name;
                }
                $this->colModel=new content_colmod($atypes);
                $colModel=$this->colModel->get();
                $_SESSION["ADP"][CCTRL."-JQX"]["colModel"]=$colModel;
            }
            header("Content-type:text/javascript");
            $this->smarty
                    ->assign("colModel", $colModel)
                    ->assign("display",$_SESSION["ADP"][CCTRL]["display"])
                    ->display("backend/00_base/grid_advanced");
        }
        function handleEvent(){
            $this->load->model("backend/type_model");
            $this->type_model=new type_model();
            $tmp=$this->type_model->getByType("content");
            $atypes=null;
            foreach ($tmp as $v){
                $atypes[]=$v->_name;
                $reatypes[$v->_name]=$v->_no;
            }
            header("Content-type:text/javascript");
            $this->smarty
                    ->assign(array("atypes"=> $atypes,"reatypes"=>$reatypes))
                    ->display("backend/".CCTRL."/handleEvent");
        }
        function edit($_id=""){
            $Data=null;
            if($_id!=""){
                $tmp=$this->backend_model->get($_id);
                if(count($tmp)>0)$Data["OBJ"]=$tmp[0];
            }
            $this->load->model("backend/type_model");
            $this->type_model=new type_model();
            $Data["otypes"]=$this->type_model->getByType("content");
            $this->smarty
                    ->assign('_SESSION', $_SESSION)
                    ->assign("Data", $Data)
                    ->display("backend/".CCTRL."/02_edit");
        }
        function save(){
            $_id    =$this->input->post("_id");
            $_alias =$this->input->post("_alias");
            $_title =$this->input->post("_title");
            $_thumb =$this->input->post("_thumb");
            $_type          =$this->input->post("_type");
            $_unitprice     =$this->input->post("_unitprice");
            //$_tag           =$this->input->post("_tag");
            $_content       =$_REQUEST["_content"];
            $messages=array();
            if($_title===false || $_title == ""){
                $messages[]    =   "[Shound be non-empty] Title không được để trống.";
            }
            if($_alias===false || convertUrl($_alias) == ""){
                $messages[]    =   "[Shound be non-empty] Alias không được để trống.";
            }
            if($_type===false || $_type == ""){
                $messages[]    =   "[Shound be non-empty] Type không được để trống.";
            }
            if($_content===false || $_content == ""){
                $messages[]    =   "[Shound be non-empty] Content không được để trống.";
            }
            if(count($messages)>0){
                $result   =   -1;
                $message    =   implode("<br/>", $messages);
                goto result;
            }
            $params=array(
                "Title"         =>  $_title,
                "Thumb"         =>  $_thumb,
                "Type"          =>  $_type,
                "Content"       =>  $_content,
                "Alias"         =>  convertUrl($_alias)
            );
            //var_dump($Params);return;
            if(!checkAuthority(CCTRL,false)){
                    $result=-1;
                    $message="[Access Denied] Bạn không có quyền thực hiện thao tác này.<br/> Mọi thay đổi dữ liệu đều không được công nhận.";
                    goto result;
            }elseif($_id==false){
                //var_dump($Params);return;
                $rs=$this->backend_model->insert($params);
            }else{
                $rs=$this->backend_model->update($_id,$params);
            }
            if($rs===false){
                $result   =   -888;
                $message    =   "[Fail] Please check your data input and try again.<br/><br/>
                    Find out more about an <span class='csp'><tt onclick='_whyerror()'>Error Message</tt></span>
                    ";
            }else{
                $alog=array(
                    "_table"=>"Product",
                    "_row"=>null,
                    "_commandtype"=>$_id==false?"insert":"update",
                    "_log"=>print_r(array(
                        "user" =>$_SESSION["ADP"]["USER"]["Name"],
                        "ip"=>  getIP(),
                        "command"=>$_id==false?"insert":"update",
                        "id"    =>$_id
                    ),true)
                );
                @$this->backend_model->tolog($alog);
                $result   =   1;
                $message    =   "[Success]. Thành công";
            }
            result:
            echo json_encode(array("result"=>$result,"message"=>$message));
        }
        function binding(){
            $data_rows=array();
            $result=$this->content_model->binding();
            for ($i=0;$i<count($result['rows']);$i++) {
                $result['rows'][$i]->Status = ($result['rows'][$i]->Status=="true" || $result['rows'][$i]->Status=="Public")?true:false;
            }
            $data[] = array(
                'TotalRows' => $result['total_rows'],
                'Rows' => $result['rows']
            );
            echo json_encode($data);
        }
        
}
?>
